const userModel = require("../models/user_model");

// 注册    userController.doRegister(user,onRetGot,onErrGot)
function doRegister(user, onRetGot, onErrGot) {
    // onRetGot({ msg: "注册成功" });
    userModel.addUser(
        user,
        (ret) => onRetGot({ ret, msg: "注册成功" }),
        (err) => onErrGot({ err, msg: "注册失败" })
    );
}

// 登录    userController.doLogin(user,onRetGot,onErrGot)
function doLogin(user, onRetGot, onErrGot) {
    // onErrGot({ msg: "登录失败" });
    userModel.getUser(
        user,
        (ret) =>
            onRetGot(
                ret ? { ret, msg: "登录成功" } : { msg: "用户名或密码错误" }
            ),
        (err) => onErrGot({ err, msg: "登录失败" })
    );
}

const userController = {
    doRegister,
    doLogin,
};

module.exports = userController;
